package com.blahti.example.drag2;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import data.ParkingDB;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.app.TimePickerDialog;
import android.widget.TimePicker;

public class ParkingInfo extends Activity{
	final ParkingInfo thisActivity = this;
	ParkingDB parking_db;
	static final int TIME_DIALOG=1;
	private int mhour;
	private int mminute;
	private int mYear;
	private int mMonth;
	private int mDay;
	private EditText EditTta;
	private TextView details;
	public String updateTime;
	public Date date2 = null;
	Date newDate=null;
	private Spinner spinner1;
	private int thiSpot;
	




	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spotinfo);
		LinearLayout mLayout =(LinearLayout) findViewById(R.id.mainLayout);
		Bundle extras = getIntent().getExtras();
		final int index = extras.getInt("spotIndex");
		thiSpot=index;
		parking_db=ParkingDB.getInstance();
		details=(TextView)this.findViewById(R.id.details);
		details.setTextColor(Color.BLACK);


		Drawable d = null;
		String hours;
		int status = 0;
		int type = 0;
		Date date = null;
		String tta = null;
		Button saveChanges = (Button)this.findViewById(R.id.saveChanges);
		 final Calendar c = Calendar.getInstance();
		 addListenerOnSpinnerItemSelection();
		 


		//TODO: get spot type (0/1/2/3)==(grey/blue-white/red-white/red-yellow)
		//TODO: get spot status
		//TODO: get time to avaialable
		if(index<5)
		{
			if((parking_db.IndexArray_A[index]!=-1)&&(parking_db.listIndex_A!=-1))
			{
				type=parking_db.parkingVector.get(parking_db.listIndex_A).list.get(parking_db.IndexArray_A[index]).ParkingType;
				status=parking_db.parkingVector.get(parking_db.listIndex_A).list.get(parking_db.IndexArray_A[index]).parkingStatus;
				tta=parking_db.parkingVector.get(parking_db.listIndex_A).list.get(parking_db.IndexArray_A[index]).timeToAvailable;
			}
		}
		else
		{
			if((parking_db.IndexArray_B[index-5]!=-1)&&(parking_db.listIndex_B!=-1))
			{
				type=parking_db.parkingVector.get(parking_db.listIndex_B).list.get(parking_db.IndexArray_B[index-5]).ParkingType;
				tta=parking_db.parkingVector.get(parking_db.listIndex_B).list.get(parking_db.IndexArray_B[index-5]).timeToAvailable;
				status=parking_db.parkingVector.get(parking_db.listIndex_B).list.get(parking_db.IndexArray_B[index-5]).parkingStatus;
			}
		}
		if(tta!=null)
		{
			try {
				date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(tta);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		    mhour = date.getHours();
		    mminute = date.getMinutes();
		    mYear=date.getYear();
			mMonth=date.getMonth();
			mDay=date.getDay();
			Log.d("read time is",tta);
			updateTime=tta.substring(tta.indexOf(" "));
			Log.d("secString",updateTime);
			

			
			
		    
		}
		ArrayAdapter myAdap = (ArrayAdapter) spinner1.getAdapter(); //cast to an ArrayAdapter
		int spinnerPosition=0;

			switch(type)
			{
			case 2:
			{
				d=getResources().getDrawable(R.drawable.parkinginfo1);
				 spinnerPosition = myAdap.getPosition("Red White");
				 details.setText("Parking is Forbidden");
				break;
			}
			case 1:
			{
				d=getResources().getDrawable(R.drawable.parkinginfo2);
				 spinnerPosition = myAdap.getPosition("Blue White");
				 details.setText("Payment Hours:\n"+ "Sun-Thu: 09:00-19:00\n"+"Fri: 09:00-13:00\n");
				break;
			}
			case 0:
			{
				d=getResources().getDrawable(R.drawable.parkingingo3);
				 spinnerPosition = myAdap.getPosition("Grey");
				 details.setText("Parking is Permitted as long as there's no forbidding traffic sign ");
				
				break;
			}
			case 3:
			{
				d=getResources().getDrawable(R.drawable.parkinginfo4);
				spinnerPosition = myAdap.getPosition("Red Yellow");
				details.setText("Parking is Forbidden- Public Transportation");
				break;
			}		
			}
			spinner1.setSelection(spinnerPosition);

		

		mLayout.setBackgroundDrawable(d);
		TextView tv =(TextView)this.findViewById(R.id.status);
		TextView TTA=(TextView)this.findViewById(R.id.TTA);
		TTA.setTextColor(Color.BLUE);
		tv.setTextColor(Color.BLUE);
		EditTta=(EditText)findViewById(R.id.ttaEdit);
	
		if(status==0)
		{
			tv.setText("Status: Available");
			EditTta.setVisibility(View.GONE);
			TTA.setVisibility(View.GONE);
			saveChanges.setVisibility(View.GONE);
		}
			
		else if (status==1)
		{
			tv.setText("Status: Not Available");
			EditTta.setText(updateTime);
			EditTta.setClickable(true);

			EditTta.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					showDialog(TIME_DIALOG);
				}
			});
		}
			
		else if (status==2)
		{
			tv.setText("Status: Unknown");
			EditTta.setVisibility(View.GONE);
			TTA.setVisibility(View.GONE);
			saveChanges.setVisibility(View.GONE);
		}
		

		saveChanges.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date now = new Date();
				now.setHours(mhour);
				now.setMinutes(mminute);
				String upd=dateFormat.format(now);
				Log.d("try2",upd);
	
			

				if(index<5)
				{
					if((parking_db.IndexArray_A[index]!=-1)&&(parking_db.listIndex_A!=-1))
						parking_db.parkingVector.get(parking_db.listIndex_A).list.get(parking_db.IndexArray_A[index]).timeToAvailable=upd;
				}
				else
				{
					if((parking_db.IndexArray_B[index-5]!=-1)&&(parking_db.listIndex_B!=-1))
						parking_db.parkingVector.get(parking_db.listIndex_B).list.get(parking_db.IndexArray_B[index-5]).timeToAvailable=upd;
				}
				SharedPreferences ref= getSharedPreferences("com.ruthy.parkApp",MODE_PRIVATE);
				SharedPreferences.Editor ed=ref.edit();
				ed.putString("SavedDialog", "show");
				ed.commit();
				finish();
								
				

			}


		});
			





	}
	public void addListenerOnSpinnerItemSelection() {
		spinner1 = (Spinner) findViewById(R.id.spinner1);
		final LinearLayout sLayout =(LinearLayout) findViewById(R.id.mainLayout);

		spinner1.setOnItemSelectedListener(new OnItemSelectedListener(){
			String typeString;
			@Override
			public void onItemSelected(AdapterView<?> parent, View arg1,int pos,long id) {
				// TODO Auto-generated method stub
				typeString=parent.getItemAtPosition(pos).toString();
				Drawable d=null;
				if(typeString.equals("Blue White"))
				{
					if(thiSpot<5)
					{
						if((parking_db.IndexArray_A[thiSpot]!=-1)&&(parking_db.listIndex_A!=-1))
						{
							d=getResources().getDrawable(R.drawable.parkinginfo2);
							parking_db.parkingVector.get(parking_db.listIndex_A).list.get(parking_db.IndexArray_A[thiSpot]).ParkingType=1;
							 details.setText("Payment Hours:\n"+ "Sun-Thu: 09:00-19:00\n"+"Fri: 09:00-13:00\n");
						}
							
					}
					else
					{
						if((parking_db.IndexArray_B[thiSpot-5]!=-1)&&(parking_db.listIndex_B!=-1))
							
						{
							d=getResources().getDrawable(R.drawable.parkinginfo2);
							parking_db.parkingVector.get(parking_db.listIndex_B).list.get(parking_db.IndexArray_B[thiSpot-5]).ParkingType=1;
							 details.setText("Payment Hours:\n"+ "Sun-Thu: 09:00-19:00\n"+"Fri: 09:00-13:00\n");
						}
							
					}
				}
				else if(typeString.equals("Red White"))
				{
					if(thiSpot<5)
					{
						if((parking_db.IndexArray_A[thiSpot]!=-1)&&(parking_db.listIndex_A!=-1))
							
						{
							d=getResources().getDrawable(R.drawable.parkinginfo1);
							parking_db.parkingVector.get(parking_db.listIndex_A).list.get(parking_db.IndexArray_A[thiSpot]).ParkingType=2;
							details.setText("Parking is Forbidden");
						}
							
					}
					else
					{
						if((parking_db.IndexArray_B[thiSpot-5]!=-1)&&(parking_db.listIndex_B!=-1))
						{
							d=getResources().getDrawable(R.drawable.parkinginfo1);
							parking_db.parkingVector.get(parking_db.listIndex_B).list.get(parking_db.IndexArray_B[thiSpot-5]).ParkingType=2;
							details.setText("Parking is Forbidden");
						}
							
					}
				}
				else if(typeString.equals("Grey"))
				{
					if(thiSpot<5)
					{
						if((parking_db.IndexArray_A[thiSpot]!=-1)&&(parking_db.listIndex_A!=-1))
							
						{
							d=getResources().getDrawable(R.drawable.parkingingo3);
							parking_db.parkingVector.get(parking_db.listIndex_A).list.get(parking_db.IndexArray_A[thiSpot]).ParkingType=0;
							details.setText("Parking is Permitted as long as there's no forbidding traffic sign ");
						}
							
					}
					else
					{
						if((parking_db.IndexArray_B[thiSpot-5]!=-1)&&(parking_db.listIndex_B!=-1))
						{
							d=getResources().getDrawable(R.drawable.parkingingo3);
							parking_db.parkingVector.get(parking_db.listIndex_B).list.get(parking_db.IndexArray_B[thiSpot-5]).ParkingType=0;
							details.setText("Parking is Permitted as long as there's no forbidding traffic sign ");
						}
							
					}
				}
				else
				{
					if(thiSpot<5)
					{
						if((parking_db.IndexArray_A[thiSpot]!=-1)&&(parking_db.listIndex_A!=-1))
						{
							d=getResources().getDrawable(R.drawable.parkinginfo4);
							parking_db.parkingVector.get(parking_db.listIndex_A).list.get(parking_db.IndexArray_A[thiSpot]).ParkingType=3;

							details.setText("Parking is Forbidden- Public Transportation");
						}
					}
					else
					{
						if((parking_db.IndexArray_B[thiSpot-5]!=-1)&&(parking_db.listIndex_B!=-1))
						{
							d=getResources().getDrawable(R.drawable.parkinginfo4);
							parking_db.parkingVector.get(parking_db.listIndex_B).list.get(parking_db.IndexArray_B[thiSpot-5]).ParkingType=3;
							details.setText("Parking is Forbidden- Public Transportation");
						}
					}
					
				}
				sLayout.setBackgroundDrawable(d);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		
		});
		

			
		
	  }



	protected Dialog onCreateDialog(int id) {
		Dialog dialog = null;;
		switch(id) {
		case TIME_DIALOG:
			 return new TimePickerDialog(this,mTimeSetListener, mhour, mminute, false);

		default:
			dialog = null;
		}
		return dialog;
	}
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    mhour = hourOfDay;
                    mminute = minute;
                    updatetime();
                }


            };
            public void updatetime()
            {
            	EditTta.setText(
                        new StringBuilder()
                                .append(pad(mhour)).append(":")
                                .append(pad(mminute))); 
            }
            
            private static String pad(int c) {
                if (c >= 10)
                        return String.valueOf(c);
                    else
                            return "0" + String.valueOf(c);
                    }





	

}
